package com.itheima.ai.controller;

import com.itheima.ai.memory.InMysqlChatMemory;
import com.itheima.ai.service.ChatHistoryService;
import com.itheima.ai.vo.MessageVO;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    @Autowired
    private ChatHistoryService chatHistoryService;


    @Autowired
    private InMysqlChatMemory chatMemory;


    @GetMapping("/{type}")
    public List<String> getHistory(@PathVariable String type) {
        return chatHistoryService.getHistory(type);
    }


    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable String type, @PathVariable String chatId) {
        //1.从chatMemory中获取会话历史信息
        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        //2.如果会话历史信息为空，则返回空列表
        if (messages==null) {
            return List.of();
        }
        //3.将会话历史信息转换为VO对象
        return messages.stream().map(message -> new MessageVO(message)).toList();

    }


}
